/*
 * This file is part of ddesktop: remote desktop system for iPad 
 *
 * Copyright (C) 2010 Kazuhiro Satoh
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, see <http://www.gnu.org/licenses/>.
 */


#import "HostListViewController.h"


@implementation HostListViewController


-(id) initWithAppStateManagerDelegate:(id)delegate setting:(Setting*)setting
{
	if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
		_setting = setting;
		[[_setting itemForKeyPath:@"HostList"] addObserver:self action:@selector(settingItemHostListIsChanged:)];

		
		{ // init navigation button
			UIBarButtonItem* addButton = [[[UIBarButtonItem alloc] initWithTitle:@"Add"
																		 style:UIBarButtonItemStylePlain
																		target:self
																		 action:@selector(navigationButtonAddHost:)] autorelease];
			UIBarButtonItem* editButton = [[[UIBarButtonItem alloc] initWithTitle:@"Edit"
																		   style:UIBarButtonItemStylePlain
																		  target:self
																		  action:@selector(navigationButtonEditHostInfo:)] autorelease];
			self.navigationItem.leftBarButtonItem = editButton;
			self.navigationItem.rightBarButtonItem = addButton;
			self.navigationItem.title = @"Hosts";
		}
		
		_appStateManagerDelegate = delegate;
		self.tableView.delegate = self;
	}
	return self;
}


- (void)dealloc{
	[super dealloc];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [[_setting directoryForKeyPath:@"HostList"] itemCount];
}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell;
	
	if (indexPath.section == 0) { // Hosts
		cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithDetailButton"];
		
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellWithDetailButton"] autorelease];
			cell.selectionStyle = UITableViewCellSelectionStyleGray;
		}
		
		SettingItem* displayName = [_setting itemForKeyPath:[NSString stringWithFormat:@"HostList._item_[%d].DisplayName", indexPath.row]];

		cell.textLabel.text = (NSString*)displayName.value;
		cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	}

	return cell;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
		SettingDirectory* hostList = [_setting directoryForKeyPath:@"HostList"];
		[hostList removeItemAtIndex:indexPath.row];
    }
	else if (editingStyle == UITableViewCellEditingStyleInsert) {
    }
}

/*
 To conform to Human Interface Guildelines, since selecting a row would have no effect (such as navigation), make sure that rows cannot be selected.
 */
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[_appStateManagerDelegate pushState:APP_STATE_CONNECT_TO_HOST_WITH_HOST_ID withParam:indexPath.row];
	return nil;
}


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
	[_appStateManagerDelegate pushState:APP_STATE_HOST_INFO_WITH_HOST_ID withParam:indexPath.row];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[_appStateManagerDelegate pushState:APP_STATE_CONNECT_TO_HOST_WITH_HOST_ID withParam:indexPath.row];
}


-(void)navigationButtonAddHost:(id)sender
{
	[_appStateManagerDelegate pushState:APP_STATE_NEW_HOST_INFO withParam:0];
}


-(void)navigationButtonEditHostInfo:(UIBarButtonItem*)sender
{
	if(self.editing){
		sender.style = UIBarButtonItemStylePlain;
		[self setEditing:NO animated:YES];
	}
	else{
		sender.style = UIBarButtonItemStyleDone;
		[self setEditing:YES animated:YES];
	}
}





-(void) settingItemHostListIsChanged:(NSObject*)value
{
	[self.tableView reloadData];
}

	

@end
